package br.com.mmsearch.hib;
// default package
// Generated 04/06/2013 00:03:37 by Hibernate Tools 4.0.0

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.mmsearch.beans.Musica;

/**
 * Home object for domain model class Musica.
 * @see .Musica
 * @author Hibernate Tools
 */
public class MusicaHIB {


	public void salvar(Musica musica) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(musica);
		transaction.commit();
		session.close();
	}

	public void excluir(Musica musica) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(musica);
		transaction.commit();
		session.close();
		
	}
	
	public Musica consultar(int idMusica) {
		Session session = HibernateUtil.getSession();
		try {
			Musica obj = (Musica) session.get(Musica.class,idMusica);
			return obj;
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Musica> listar(){
		Session session = HibernateUtil.getSession();
		try{
			return session.createCriteria(Musica.class).list();
		}finally{
			session.close();
		}
	}
	
	public boolean isMusicaCadastrada(Musica musica){
		Set<Musica> musicas = new HashSet<Musica>();
		musicas.addAll(listar());
		if(musicas.contains(musica))
			return true;
		return false;
	}

	public Musica consultarByObject(Musica musica) {
		Session session = HibernateUtil.getSession();
		try{
			Musica obj = (Musica) session.createCriteria(Musica.class).
				   add(Restrictions.eq("this.musica", musica.getMusica())).
				   add(Restrictions.eq("this.escritores", musica.getEscritores())).
				   add(Restrictions.eq("this.realizadores", musica.getRealizadores())).
				   add(Restrictions.eq("this.escritoresRealizadores", musica.getEscritoresRealizadores())).
				   setMaxResults(1).
				   uniqueResult();
			return obj;
		}finally{
			session.close();
		}
	}
}
